class commond{
	constructor(){
		// this.servDomain='https://web.anzhimo.com';
		this.servDomain='https://app.diaochan88.com';
		// this.servDomain='https://app.yuyuedaojia6.com';
	}
	tips(title){
		uni.showToast({
			title,
			icon:"none"
		});
	}
	http(path,param=null,method="GET",header={}){
		const that=this;
		let headers={
			'content-type': 'application/json',
		}
		
		const login=uni.getStorageSync('login');
		const location=uni.getStorageSync('location');
		if(login)
			headers.token=login.token;
		return new Promise((resolve,reject)=>{
			uni.showLoading({
				title:'loading...',
				mask:true
			});
			uni.request({
				url:`${that.servDomain}${path}`,
				method:method,
				header:Object.assign(headers,header),
				data:param,
				success: (res) => {
					if(res.data.code==-1){
						uni.showToast({
							icon:"none",
							title:res.data.msg,
							mask:true,
							duration:1500
						});
						setTimeout(()=>{
							uni.navigateTo({
								url:'/pages/index/login'
							});
						},1500);
						return;
					}else if(res.data.code==0){
						uni.showToast({
							icon:"none",
							title:res.data.msg,
							mask:true,
							duration:1500
						});
						reject(new Error(res.data.msg));
						return;
					}
					resolve(res.data);
				},fail: (err) => {
					console.log(err);
					uni.showToast({
						icon:"error",
						title:'网络连接失败',
						mask:true,
						duration:1500
					});
					reject(err);
				},complete: () => {
					uni.hideLoading();
				}
			});
		});
	}
	formatTime(time,fmtStr=0){
		var date =new Date(time * 1000),
			year = date.getFullYear(),
			month = date.getMonth() + 1,
			day = date.getDate(),
			hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
			minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
			second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
			month >= 1 && month <= 9 ? (month = "0" + month) : "";
			day >= 0 && day <= 9 ? (day = "0" + day) : "";
			
			return {year,month,day,hour,minute,second}
			
		// if(fmtStr==0){
		// 	return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
		// }else if(fmtStr==1){
		// 	return year + '-' + month + '-' + day + ' ' + hour + ':' + minute;	
		// }
		// else{
		// 	return year + '-' + month + '-' + day
		// }
	}
	getAddressForPos(lat,lng){
		return new Promise((resolve,reject)=>{
			const script = document.createElement('script');
			script.src = `https://apis.map.qq.com/ws/geocoder/v1/?location=${lat},${lng}&output=jsonp&key=KSNBZ-A243Z-74IX2-THX4G-CULW6-2GB5L&callback=callback`;
			const callbackName = 'callback';
			window[callbackName] = function(data) {
				document.body.removeChild(script);
				delete window[callbackName];
				if (data.status == 0) {
					resolve(data.result.address_component);
				} else {
					resolve({});
				}
			};
			document.body.appendChild(script);
		});
	}
	getLocalPos(){
		return new Promise((resolve,reject)=>{
			uni.getLocation({
				type: 'wgs84',
				success: (res) => {
					const location={"lat":res.latitude,"lng":res.longitude};
					resolve(location);
				},
				fail(e) {
					console.log(e);
					reject(e);
				}
			});
		});
	}
	copy(content){
		//#ifndef H5
		uni.setClipboardData({
			data: String(content), // 必须字符串
			success: function () {
				console.log('success');
			}
		});
		//#endif
	
		// h5端赋值方法，使用创建节点
		// #ifdef H5
		if (!document.queryCommandSupported('copy')) { // 兼容某些浏览器的判断
			console.log('该浏览器不支持')
		}
		let textarea = document.createElement("textarea");
		textarea.value = content;
		textarea.readOnly = "readOnly";
		document.body.appendChild(textarea);
		textarea.select(); // 选择对象
		textarea.setSelectionRange(0, content.length); // 核心
		let result = document.execCommand("copy"); // 执行浏览器复制命令
		if (result) {
			uni.showToast({
				title: '复制成功',
				duration: 2000
			});
		}
		textarea.remove();
		// #endif
	
	}
	
	
	
}
module.exports=commond;